﻿using System;
using System.Runtime.InteropServices;

namespace ActiveDirectoryUtilities.DirectoryServiceAccess
{
	public class NetlogonInfo1
	{
		/// <summary>
		/// http://msdn.microsoft.com/en-us/library/bb432374(VS.85).aspx
		/// </summary>
		[StructLayout(LayoutKind.Sequential)]
		internal sealed class NETLOGON_INFO_1
		{
			public int netlog1_flags;
			public int netlog1_pdc_connection_status;
		}

		private NETLOGON _flags;

		public NETLOGON Flags { get { return _flags; } }
		internal NetlogonInfo1(NETLOGON_INFO_1 info)
		{
			_flags = (NETLOGON)info.netlog1_flags;
		}
	}

	/// <summary>
	/// http://msdn.microsoft.com/en-us/library/bb432375(VS.85).aspx
	/// </summary>
	[StructLayout(LayoutKind.Sequential)]
	internal sealed class NETLOGON_INFO_2
	{
		public int netlog2_flags;
		public int netlog2_pdc_connection_status;
		public IntPtr netlog2_trusted_dc_name;
		public int netlog2_tc_connection_status;
	}

	/// <summary>
	/// http://msdn.microsoft.com/en-us/library/bb432376(VS.85).aspx
	/// </summary>
	[StructLayout(LayoutKind.Sequential)]
	internal sealed class NETLOGON_INFO_3
	{
		public int netlog1_flags;
		public int netlog3_logon_attempts;
		public int netlog3_reserved1;
		public int netlog3_reserved2;
		public int netlog3_reserved3;
		public int netlog3_reserved4;
		public int netlog3_reserved5;
	}

	/// <summary>
	/// http://msdn.microsoft.com/en-us/library/bb432377(VS.85).aspx
	/// </summary>
	[StructLayout(LayoutKind.Sequential)]
	internal sealed class NETLOGON_INFO_4
	{
		public IntPtr netlog4_trusted_dc_name;
		public IntPtr netlog4_trusted_domain_name;
	}

	/// <summary>
	/// http://msdn.microsoft.com/en-us/library/cc237266.aspx
	/// </summary>
	public enum NETLOGON_CONTROL
	{
		QUERY = 1,
		REPLICATE = 2,
		SYNCHRONIZE = 3,
		PDC_REPLICATE = 4,
		REDISCOVER = 5,
		TC_QUERY = 6,
		TRANSPORT_NOTIFY = 7,
		FIND_USER = 8,
		CHANGE_PASSWORD = 9,
		TC_VERIFY = 10,
		FORCE_DNS_REG = 11,
		QUERY_DNS_REG = 12,
		BACKUP_CHANGE_LOG = 0x0000FFFC,
		TRUNCATE_LOG = 0x0000FFFD,
		SET_DBFLAG = 0x0000FFFE,
		BREAKPOINT = 0x0000FFFF
	}

	/// <summary>
	/// http://msdn.microsoft.com/en-us/library/bb432375(VS.85).aspx
	/// </summary>
	public enum NETLOGON
	{
		REPLICATION_NEEDED = 0x00000001,
		REPLICATION_IN_PROGRESS = 0x00000002,
		FULL_SYNC_REPLICATION = 0x00000003,
		REDO_NEEDED = 0x00000008,
		HAS_IP = 0x00000010,
		HAS_TIMESERV = 0x00000020,
		DNS_UPDATE_FAILURE = 0x00000040,
		VERIFY_STATUS_RETURNED = 0x00000080
	}

	public enum NETLOGON_QUERY_LEVEL
	{
		NETLOGON_INFO_1,
		NETLOGON_INFO_2,
		NETLOGON_INFO_3,
		NETLOGON_INFO_4
	}
	public class I_NetlogonControl2
	{
		//public static NetlogonInfo1 NetLogonControl(string serverName, NETLOGON_CONTROL functionCode, out NetlogonInfo1 ni1)
		//{
		//    NetlogonInfo1.NETLOGON_INFO_1 temp;
		//    int res = I_NetLogonControl2(serverName, functionCode, 1, null, out temp);
		//    if (res != 0)
		//    {
		//        return new NetlogonInfo1(temp);
		//    }
		//    return null;
		//}

		//public static int NetLogonControl(string serverName, NETLOGON_CONTROL functionCode, out NetlogonInfo1 ni1)
		//{
		//}

		//public static int NetLogonControl(string serverName, NETLOGON_CONTROL functionCode, out NetlogonInfo1 ni1)
		//{
		//}

		//public static int NetLogonControl(string serverName, NETLOGON_CONTROL functionCode, out NetlogonInfo1 ni1)
		//{
		//}

		[DllImport("netapi32.dll", CharSet = CharSet.Unicode)]
		private static extern int I_NetLogonControl2(string serverName, int FunctionCode, int QueryLevel, IntPtr data, out IntPtr buffer);
	}
}
